package com.ebt.m.proposal_v2.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ebt.m.R;
import com.ebt.m.proposal_v2.dao.response.ResponseAttach;
import com.ebt.m.proposal_v2.helper.ThemeHelper;
import com.ebt.m.proposal_v2.widget.callback.OnRecyclerItemClickListener;
import com.ebt.m.utils.UIHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * @author archer.qi
 *         Created on 2017/5/10.
 */
public class EBTMultiListDialogAdapter extends RecyclerView.Adapter<EBTMultiListDialogAdapter.ViewHolder> {

    private Context mContext;
    private List<ResponseAttach> mDataList;
    private LayoutInflater mInflater;

    private OnRecyclerItemClickListener mOnItemClickListener;

    public EBTMultiListDialogAdapter(Context context, List<ResponseAttach> data) {
        mContext = context;
        mDataList = data;
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public List<ResponseAttach> getChecked() {
        if (mDataList == null || mDataList.size() == 0) {
            return null;
        }
        List<ResponseAttach> check_data = new ArrayList<>();
        for (ResponseAttach ra : mDataList) {
            if (ra != null && ra.isChecked) {
                check_data.add(ra);
            }
        }
        return check_data;
    }

    public void setOnItemClickListener(OnRecyclerItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    public ResponseAttach getItemData(int position) {
        if (mDataList != null && mDataList.size() > 0) {
            if (position >= 0 && position < mDataList.size()) {
                return mDataList.get(position);
            }
        }
        return null;
    }

    @Override
    public EBTMultiListDialogAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(mInflater.inflate(R.layout.item_dialog_ebt_list_multi, parent, false));
    }

    @Override
    public void onBindViewHolder(EBTMultiListDialogAdapter.ViewHolder holder, int position) {
        ResponseAttach attach = getItemData(position);
        if (attach == null) {
            return;
        }
        holder.setText(attach.getDisplayName());
        holder.setCheck(attach.isChecked);
        holder.setEnable(attach.isEnable == 1); // isEnable == 1 符合条件
        holder.setClickListener(position);
    }

    @Override
    public int getItemCount() {
        if (mDataList != null && mDataList.size() > 0) {
            return mDataList.size();
        }
        return 0;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView name;
        private ImageView status;
        private LinearLayout root;

        public ViewHolder(View itemView) {
            super(itemView);
            if (itemView != null) {
                name = (TextView) itemView.findViewById(R.id.EBTListDialog_listItem_name);
                status = (ImageView) itemView.findViewById(R.id.EBTListDialog_listItem_status);
                root = (LinearLayout) itemView.findViewById(R.id.root);
            }
        }

        public void setText(String text) {
            if (name != null) {
                name.setText(text);
            }
        }

        public void setCheck(boolean check) {
            if (status != null) {
                status.setImageResource(check ? R.drawable.ic_checked_circle : R.drawable.ic_uncheck_circle);
            }
        }

        public void setEnable(boolean enable) {
            int color;
            if (enable) {
                color = ThemeHelper.getColor(mContext, R.color.PrimaryContentColor);
            } else {
                color = ThemeHelper.getColor(mContext, R.color.PrimaryContentColorLight);
            }
            name.setTextColor(color);
        }

        public void setClickListener(int position) {
            itemView.setOnClickListener(new OnClick(position));
        }

        private class OnClick implements View.OnClickListener {
            private int position;

            public OnClick(int pos) {
                position = pos;
            }

            @Override
            public void onClick(View v) {
                if (mDataList.get(position) == null) {
                    return;
                }
                if (mDataList.get(position).isEnable == 0) {
                    // 不符合条件，提示
                    UIHelper.makeToast(mContext, mDataList.get(position).warnMsg);
                    return;
                }
                boolean isChecked = mDataList.get(position).isChecked;
                mDataList.get(position).isChecked = !isChecked;
                notifyDataSetChanged();

                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(itemView, position);
                }
            }
        }
    }

}
